#include "GameInputListener.h"




void InputListener::finsh()
{
    spdlog::debug("InputListener stopping");
    while (shouldExit != true)
    {
        Sleep(10);
    }
    inputThread.join();
    isInputListenerExitReady = true;
    spdlog::debug("InputListener stopped");
}

void InputListener::startListening()
{
    // 在子线程中启动监听
    inputThread = std::thread([this]()
                              {
            while (!shouldExit) {
                checkMouseInput();
                Sleep(10); // 延迟一段时间，避免过快的循环
            }
            spdlog::debug("InputListener work done, prepare to stop"); });
}

bool InputListener::GetInput(int keycode)
{
    for (size_t i = 0; i < inputqueue.size(); i++)
    {
        if (keycode == inputqueue.at(i))
        {
            return true;
        }
    }
    return false;
}

void InputListener::cleanInput()
{
    inputqueue.clear();
}

void InputListener::keyMsgHandle(ExMessage msg)
{
    char *status = (msg.message == WM_KEYDOWN ? "press  " : "release");
    char *prestatus = (msg.prevdown ? "press  " : "release");
    bool flag = msg.message == WM_KEYDOWN;
    bool preflag = msg.prevdown;

    if (flag != preflag)
    {
        spdlog::debug("mgs.vkcode = {}", msg.vkcode);
        inputqueue.push_back(msg.vkcode);
        switch (msg.vkcode)
        {
        case VK_BACK: // BACKSPACE
            spdlog::debug("BACKSPACE Key {}, pre status {}", status, prestatus);
            break;
        case VK_TAB: // Tab
            spdlog::debug("Tab Key {}, pre status {}", status, prestatus);
            break;
        case VK_RETURN: // Enter
            spdlog::debug("Enter Key {}, pre status {}", status, prestatus);
            break;
        case VK_SHIFT:
            spdlog::debug("Shift Key {}, pre status {}", status, prestatus);
            break;
        case VK_CONTROL:
            spdlog::debug("CTRL Key {}, pre status {}", status, prestatus);
            break;
        case VK_MENU:
            spdlog::debug("Alt Key {}, pre status {}", status, prestatus);
            break;
        case VK_CAPITAL:
            spdlog::debug("CAPS LOCK  Key {}, pre status {}", status, prestatus);
            break;
        case VK_ESCAPE:
            spdlog::debug("ESC Key {}, pre status {}", status, prestatus);
            break;
        case VK_SPACE:
            spdlog::debug("SPACE Key {}, pre status {}", status, prestatus);
            break;
        case VK_LEFT:
            spdlog::debug("LEFT Key {}, pre status {}", status, prestatus);
            break;
        case VK_UP:
            spdlog::debug("UP Key {}, pre status {}", status, prestatus);
            break;
        case VK_RIGHT:
            spdlog::debug("RIGHT Key {}, pre status {}", status, prestatus);
            break;
        case VK_DOWN:
            spdlog::debug("DOWN Key {}, pre status {}", status, prestatus);
            break;
        case 0x51:
            spdlog::debug("Q Key {}, pre status {}", status, prestatus);
            shouldExit = true;
            break;
        default:
            break;
        }
    }
}

void InputListener::checkMouseInput()
{
    ExMessage msg;
    //  使用非阻塞式
    if (peekmessage(&msg, EX_MOUSE | EX_KEY | EX_WINDOW))
    {
        // Handle different mouse events
        switch (msg.message)
        {
        case WM_MOUSEMOVE:
            spdlog::debug("Mouse Moved: ({}, {})", msg.x, msg.y);
            break;
        case WM_LBUTTONDOWN:
            spdlog::debug("Left Button Down at: ({}, {})", msg.x, msg.y);
            break;
        case WM_LBUTTONUP:
            spdlog::debug("Left Button Up at: ({}, {})", msg.x, msg.y);
            break;
        case WM_RBUTTONDOWN:
            spdlog::debug("Right Button Down at: ({}, {})", msg.x, msg.y);
            break;
        case WM_RBUTTONUP:
            spdlog::debug("Right Button Up at: ({}, {})", msg.x, msg.y);
            break;
        case WM_MBUTTONDOWN:
            spdlog::debug("Middle Button Down at: ({}, {})", msg.x, msg.y);
            break;
        case WM_MBUTTONUP:
            spdlog::debug("Middle Button Up at: ({}, {})", msg.x, msg.y);
            break;
        case WM_MOUSEWHEEL:
            spdlog::debug("Mouse Wheel: {}", GET_WHEEL_DELTA_WPARAM(msg.wheel));
            break;
        case WM_KEYDOWN:
        case WM_KEYUP:
            keyMsgHandle(msg);
            break;
        case WM_CLOSE:
            spdlog::debug("Window closed");
            shouldExit = true;
            break;
        default:
            break;
        }
    }

    FlushMouseMsgBuffer(); // Clear the mouse message buffer
}